jQuery(function(event) {
	
	$('#locationsList input[type="button"].butChange').click(function(e){
		var td = $(this).parents('tr').children();
		var tdt = td.eq(2);
		var tdb = td.eq(3);
		
		var val = tdt.text();
		tdt.html('');
		
		var inp = $('<input type="text" />');
		inp.val(val);
		inp.attr('title', val);
		tdt.append(inp);
		
		var butEdit = $(this);
		butEdit.hide();

		var funCloseEditing = function(e){
			tdb.find('input[type="button"]').not('.butChange').remove();
			butEdit.show();
		};
		
		$('<input class="button-ok" type="button" value="' + 'BTN_OK'.translate() + '" />').click(function(e){
			var newVal = inp.val().replace(/^[\s\t\r\n]+/g, '').replace(/[\s\t\r\n]+$/g, '');
			if(newVal.length > 0 && newVal != val)
			{
				var metaVal = td.eq(1).metadata();
				$.ajax({
					url: '/a_ajax/location/translation.saveCityTranslation',
					method: 'post',
					timeout: 20000,
					async: true,
					data: {
						cityName: metaVal.cityName,
						cityNameTranslation: newVal,
						language: $('select[name="sTranslationLang"]').val()
					},
					success: function(data){
						if(data == 'success')
						{
							inp.remove();
							var name = tdb.parent().children().eq(1).text();
							$('#locationsList').find('tr').each(function(){
								var trtds = $(this).find('td');
								if(trtds.eq(1).text().toLowerCase() == name.toLowerCase())
								{
									trtds.eq(2).removeAttr('style').text(newVal);
								} 
							});
							funCloseEditing();
						}
						else
						{
							alert('Translation saving error!');
						}							
					},
					error: function(){
						alert('Translation saving error!');
					}
				});
			}
			else
			{
				alert('Please define a new translation before saving the city.');
				inp.focus();
			}
		}).appendTo(tdb);
		
		$('<input class="button-cancel" type="button" value="' + 'BTN_CANCEL'.translate() + '" />').click(function(){
			var val = inp.attr('title');
			inp.remove();
			tdt.text(val);
			funCloseEditing();
		}).appendTo(tdb);
		
	})
	;

	
	$('#locationsList input[type="button"].butRegion').click(function(e){
		var butRegion = $(this);
		butRegion.hide();
		var tdb = butRegion.parent();
		var tdr = tdb.parent().children().eq(4);
		var mainCity = tdr.text();
		tdr.text('');
		var selectRegCities = $('select.regCities').clone().removeClass().appendTo(tdr).show();
		var name = tdb.parent().children().eq(1).text();
		selectRegCities.val(mainCity);
		selectRegCities.find('option[value="' + name + '"]').remove();

		var funCloseRegion = function(e){
			tdb.children().not('.butRegion').remove();
			butRegion.show();
		};
		
		$('<input class="button-ok" type="button" value="' + 'BTN_OK'.translate() + '" />').click(function(e){
			var newMainCity = selectRegCities.val();
			$.ajax({
				url: '/a_ajax/location/translation.setRegionOwnerCity',
				method: 'post',
				timeout: 20000,
				async: true,
				data: {
					cityName: name,
					ownerCityName: newMainCity,
					countryName: $('select[name="sCountryName"]').val()
				},
				success: function(data){
					if(data == 'success')
					{
						var newVal = selectRegCities.val();
						$('#locationsList').find('tr').each(function(){
							var trtds = $(this).find('td');
							if(trtds.eq(1).text().toLowerCase() == name.toLowerCase())
							{
								trtds.eq(4).text(newVal);
							} 
						});
						funCloseRegion();
					}
					else
					{
						alert('Region saving error!');
					}							
				},
				error: function(){
					alert('Region saving error!');
				}
			});
		}).appendTo(tdb);
		
		$('<input class="button-cancel" type="button" value="' + 'BTN_CANCEL'.translate() + '" />').click(function(){
			tdr.text(mainCity);
			funCloseRegion();
		}).appendTo(tdb);
		
	});
	
	
	$('#locationsList input[type="button"].butAlias').click(function(e){
		var butAlias = $(this);
		butAlias.hide();
		var tdb = butAlias.parent();
		var tdr = tdb.parent().children().eq(6);
		var mainCity = tdr.text();
		tdr.text('');
		var selectRegCities = $('select.regCities').clone().removeClass().appendTo(tdr).show();
		var name = tdb.parent().children().eq(1).text();
		selectRegCities.val(mainCity);
		selectRegCities.find('option[value="' + name + '"]').remove();
/*		var vendorId = tdb.parent().children().eq(1).metadata().vendorId;
		selectRegCities.children().filter(function(i){
			return ($(this).metadata().vendorId != vendorId);
		}).remove();*/

		var funCloseAlias = function(e){
			tdb.children().not('.butAlias').remove();
			butAlias.show();
		};
		
		$('<input class="button-ok" type="button" value="' + 'BTN_OK'.translate() + '" />').click(function(e){
			var newMainCity = selectRegCities.val();
/*			var metaVal = tdb.parent().children().eq(1).metadata();*/
			$.ajax({
				url: '/a_ajax/location/translation.setAliasCity',
				method: 'post',
				timeout: 20000,
				async: true,
				data: {
					cityName: name,
					aliasCityName: newMainCity,
					countryName: $('select[name="sCountryName"]').val()
/*					vendorId: metaVal.vendorId,
					vendorCountryCode: metaVal.vendorCountryCode*/
				},
				success: function(data){
					if(data == 'success')
					{
						var newVal = selectRegCities.val();
						$('#locationsList').find('tr').each(function(){
							var trtds = $(this).find('td');
							if(trtds.eq(1).text().toLowerCase() == name.toLowerCase())
							{
								trtds.eq(6).text(newVal);
							} 
						});
						funCloseAlias();
					}
					else
					{
						alert('Alias saving error!');
					}							
				},
				error: function(){
					alert('Alias saving error!');
				}
			});
		}).appendTo(tdb);
		
		$('<input class="button-cancel" type="button" value="' + 'BTN_CANCEL'.translate() + '" />').click(function(){
			tdr.text(mainCity);
			funCloseAlias();
		}).appendTo(tdb);
		
	});
	
		
	$('#locationsList input[type="checkbox"].butRate').click(function(e){
		var but = $(this);
		
		var funRevert = function(e){
 			if(e.attr('checked'))
			{
				e.removeAttr('checked');
				e.next('a').find('img').removeClass('checked');
			}
			else
			{
				e.next('a').find('img').addClass('checked');
				e.attr('checked', 'checked');
			}
		};
		$.ajax({
			url: '/a_admin/location/translation.changeSystemCityRate',
			method: 'post',
			timeout: 20000,
			dataType: 'json',
			async: false,
			data: {
				cityId: but.val(),
				rated: (but.attr('checked') ? 'Y' : 'N')
			},
			success: function(data){
				if(data.status != undefined)
				{
					if(data.status != 'success')
					{
						alert('Rate changing error: ' + data.message);
						funRevert(but);
					}
				}
				else
				{
					alert('Rate changing error!');
					funRevert(but);
				}							
			},
			error: function(){
				alert('Rate changing error!');
				funRevert(but);
			}
		});
		
	});
});
